<!--
 * @Descripttion: 退款详情
 * @Author: xiao li
 * @Date: 2021-03-11 15:42:01
 * @LastEditors: xiao li
 * @LastEditTime: 2022-01-25 10:16:00
-->

<template>
  <div class="lb-shop-order-edit">
    <top-nav :isBack="true" />
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        label-width="130px"
        size="mini"
      >
        <lb-classify-title title="客户信息"></lb-classify-title>
        <div class="space-lg"></div>
        <div class="space-lg"></div>
        <el-form-item label="客户ID：">
          <div>{{ subForm.user_id }}</div>
        </el-form-item>
        <el-form-item label="微信昵称：">
          <div>{{ subForm.user_info.nickName }}</div>
        </el-form-item>
        <el-form-item label="客户手机号：" v-if="subForm.phone">
          <div>{{ subForm.phone }}</div>
        </el-form-item>
        <div class="space-lg"></div>
        <lb-classify-title title="预订信息"></lb-classify-title>
        <div class="space-lg"></div>
        <div class="space-lg"></div>
        <el-form-item label="预订时间：">
          <div>
            {{ subForm.time_date | handleTime(1) }}（{{ subForm.week }}）
            {{ subForm.time_type }}
          </div>
        </el-form-item>
        <el-form-item
          :label="`${item.key}：`"
          v-for="(item, index) in subForm.content"
          :key="index"
        >
          <div v-if="item.type !== 3 || (item.type === 3 && !item.value)">
            {{ item.value || '无' }}
          </div>
          <div class="flex-warp" v-if="item.type === 3 && item.value">
            <lb-cover
              :fileList="[{ url: item.value }]"
              :isToDel="false"
              fileType="image"
              type="more"
              size="small"
              :fileSize="1"
            ></lb-cover>
          </div>
        </el-form-item>
        <div class="space-lg"></div>
        <lb-classify-title title="订单信息"></lb-classify-title>
        <div class="space-lg"></div>
        <div class="space-lg"></div>
        <el-form-item label="付款订单号：" v-if="subForm.pay_order_code">
          <div>{{ subForm.pay_order_code }}</div>
        </el-form-item>
        <el-form-item label="退款订单号：" v-if="subForm.out_refund_no">
          <div>{{ subForm.out_refund_no }}</div>
        </el-form-item>
        <el-form-item label="申请时间：">
          <div>{{ subForm.create_time | handleTime }}</div>
        </el-form-item>
        <el-form-item label="处理时间：" v-if="subForm.status > 1">
          <div>{{ subForm.refund_time | handleTime }}</div>
        </el-form-item>
        <el-form-item label="订单状态：">
          <div>
            {{ statusType[subForm.status] }}
          </div>
        </el-form-item>
        <el-form-item label="退款金额：" v-if="subForm.status === 2">
          <div class="c-warning">
            {{ `¥${subForm.refund_price}` }}
          </div>
        </el-form-item>
        <el-form-item label="支付方式：">
          <div>{{ subForm.balance * 1 > 0 ? '储值支付' : '微信支付' }}</div>
        </el-form-item>
        <el-form-item label="场地所属公司：">
          <div>{{ subForm.company_name || '' }}</div>
        </el-form-item>
        <el-form-item label="员工：">
          <div>{{ subForm.staff_name || '' }}</div>
        </el-form-item>
        <div class="space-lg"></div>
        <lb-classify-title title="退款信息"></lb-classify-title>
        <div class="space-lg"></div>
        <div class="space-lg"></div>
        <el-form-item label="退款原因：">
          <div v-html="subForm.text"></div>
        </el-form-item>
        <el-form-item
          label="退款图片："
          v-if="subForm.imgs && subForm.imgs.length > 0"
        >
          <div class="flex-warp">
            <lb-cover
              :fileList="subForm.imgs"
              :isToDel="false"
              fileType="image"
              type="more"
              size="small"
              :fileSize="subForm.imgs.length"
            ></lb-cover>
          </div>
        </el-form-item>
        <el-form-item label="处理退款：" v-show="subForm.status === 1">
          <lb-button size="mini" plain type="danger" @click="toRefuse"
            >拒绝退款</lb-button
          >
          <lb-button size="mini" plain type="success" @click="showRefundDialog"
            >立即退款</lb-button
          >
        </el-form-item>
      </el-form>
      <div class="space-lg"></div>
      <lb-classify-title title="退款场地"></lb-classify-title>
      <div class="space-lg"></div>
      <el-table
        v-loading="loading"
        :data="[subForm]"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="place_cover" label="封面图">
          <template slot-scope="scope">
            <lb-image :src="scope.row.place_cover" />
          </template>
        </el-table-column>
        <el-table-column prop="place_title" label="场地名称"></el-table-column>
        <el-table-column prop="table_text" label="容纳桌数"> </el-table-column>
        <el-table-column prop="place_price" label="预付金">
          <template slot-scope="scope"> ¥{{ subForm.place_price }} </template>
        </el-table-column>
      </el-table>
      <lb-button type="primary" @click="$router.back(-1)">{{
        $t('action.back')
      }}</lb-button>

      <el-dialog title="退款" :visible.sync="dialogRefund" width="400px" center>
        <div class="refund-inner">
          <lb-tips>请核对信息后输入需要退款的金额</lb-tips>
          <el-input
            :disabled="refundTotalMoney * 1 === 0"
            v-model="refundMoney"
            placeholder="请输入退款金额"
          ></el-input>
          <div class="mt-md f-caption c-caption">
            <p class="mt-md">
              实际支付的金额
              <span class="c-warning">￥{{ refundTotalMoney }}</span>
            </p>
            <p>退款金额不能大于订单实际支付金额</p>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogRefund = false">{{
            $t('action.cancel')
          }}</el-button>
          <el-button type="primary" @click="toPassRefund">确认退款</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    return {
      loading: false,
      navTitle: '',
      statusType: {
        1: '退款中',
        2: '退款成功',
        3: '退款失败'
      },
      subForm: {
        id: 0
      },
      dialogRefund: false,
      refundMoney: '',
      lockRefund: false
    }
  },
  created () {
    let { id } = this.$route.query
    this.subForm.id = id
    this.getDetail()
  },
  methods: {
    async getDetail () {
      let { id } = this.subForm
      let { code, data } = await this.$api.banquet.refundInfo({ id })
      if (code !== 200) return
      data.imgs = data.imgs && data.imgs.length > 0 ? data.imgs.map(item => {
        return { url: item }
      }) : []
      data.text = data.text.replace(/\n/g, '<br>')
      this.subForm = data
    },
    showRefundDialog () {
      this.refundTotalMoney = this.subForm.place_price
      this.refundMoney = this.subForm.place_price
      this.dialogRefund = true
    },
    /**
     * @method: 同意退款
     */
    async toPassRefund (type) {
      if (this.lockRefund) return
      this.lockRefund = true
      let { id } = this.subForm
      let price = this.refundMoney * 1
      let refundTotalMoney = this.refundTotalMoney * 1
      let param = { id, price, text: '' }
      let reg = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/
      if ((refundTotalMoney === 0 && price === 0) || (price > 0 && price <= refundTotalMoney && reg.test(price))) {
        let { code } = await this.$api.banquet.passRefund(param)
        this.lockRefund = false
        if (code !== 200) return
        this.$message.success(this.$t('tips.successSub'))
        this.dialogRefund = false
        this.refundMoney = ''
        this.getDetail()
      } else {
        this.lockRefund = false
        this.$message.error('请核对金额再提交！')
      }
    },
    /**
     * @method: 拒绝退款
     */
    toRefuse () {
      this.$confirm(this.$t('tips.confirmNoRefund'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        let { id } = this.subForm
        this.$api.banquet.noPassRefund({ id }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successSub'))
            this.getDetail()
          }
        })
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-shop-order-edit {
  .el-form {
    .el-image {
      width: 120px;
      height: 120px;
    }
  }
  .el-textarea {
    width: 600px;
  }
}
</style>
